// pages/binding/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    productsList:[],
    // scene:"undefined",
  },

  //解除绑定
  unbindHandle:function(e){
    let that = this;
    wx.showModal({
      title: '提示',
      content: "确定解绑该产品么？",
      confirmColor:"#E65F5B",
      success(res){
        if(res.confirm){
          var code = e.currentTarget.dataset.code;
          const data = {
            code:code
          }
          app.getData('artware/unbind','GET',data).then(res =>{
            that.getProductsList()
          })
        }
      }
    })
  },

  //微信小程序添加商品
  scanQrcode:function(){
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: function(res) {
        const url = res.path
        if(url != ''){
          const code = url.split('=')[1];
          that.bingArtware(code)
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: "请扫商品码"
          })
        }
      }
    })
  },

  //点击产品跳转到ar扫码
  jumpScanQr:function(e){
    const data = e.currentTarget.dataset.info;
    app.navigateToUrl('/pages/index/index',data)
  },

  bingArtware:function(code){
    var data = {
      code:code
    }
    app.getData('artware/bind','GET',data).then(res =>{
      const { data } = res;
      app.navigateToUrl('/pages/index/index',data)
    })
  },

  getProductsList:function(){
    const data = {
      page:1,
      limit:50
    }
    app.getData('artware/page','GET',data).then(res =>{
      const { data } = res;
      this.setData({
        productsList:data.records,
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const scene = decodeURIComponent(options.scene)
    if(scene != "undefined"){
      this.bingArtware(scene)
    }
  },
  /**
   * 跳转到产品码页面
   */
  lookHandle:function(){
    wx.navigateTo({
      url: '/pages/product-code/index',
    })
  },

  // loadPage:function(){
  //   this.getProductsList()
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    this.getProductsList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})